package com.yanqu.road.dao.impl.activity.springunionbanquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.springunionbanquet.SpringUnionBanquetUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SpringUnionBanquetUserDataDaoImpl extends TempDataObjectDao<SpringUnionBanquetUserData> {

    public static String getSpringUnionBanquetUserDataSql = "select * from `t_c_spring_union_banquet_user` where `activityId`=?";
    public Map<Long, SpringUnionBanquetUserData> getSpringUnionBanquetUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SpringUnionBanquetUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getSpringUnionBanquetUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SpringUnionBanquetUserData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateSpringUnionBanquetUserDataSql = "update `t_c_spring_union_banquet_user` set unionUid=?,openNum=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SpringUnionBanquetUserData springUnionBanquetUserData) {
        springUnionBanquetUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, springUnionBanquetUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, springUnionBanquetUserData.getOpenNum()));
        para.put(i++, new DbParameter(Types.INTEGER, springUnionBanquetUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSpringUnionBanquetUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSpringUnionBanquetUserDataSql = "insert into `t_c_spring_union_banquet_user`(activityId,userId,unionUid,openNum) values (?,?,?,?)";
    @Override
    protected boolean addData(SpringUnionBanquetUserData springUnionBanquetUserData) {
        springUnionBanquetUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, springUnionBanquetUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, springUnionBanquetUserData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, springUnionBanquetUserData.getOpenNum()));
        if (getDb().executeNoneQuery(insertSpringUnionBanquetUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SpringUnionBanquetUserData parseData(ResultSet rs) throws SQLException {
        SpringUnionBanquetUserData springUnionBanquetUserData = new SpringUnionBanquetUserData();
        springUnionBanquetUserData.setActivityId(rs.getInt("activityId"));
        springUnionBanquetUserData.setUserId(rs.getLong("userId"));
        springUnionBanquetUserData.setUnionUid(rs.getString("unionUid"));
        springUnionBanquetUserData.setOpenNum(rs.getInt("openNum"));

        return springUnionBanquetUserData;
    }
}
